package uz.rt.physics;

import uz.rt.units.Energy;
import uz.rt.units.Length;

public class Tpr extends Curve<Length, Double> implements Comparable<Tpr>
{
// ------------------------------ FIELDS ------------------------------

    /**
     * Energy (MV / MeV) at which the TPR was measured
     */
    private Energy energy;
    /**
     * Equivalent square field size at reference depth
     */
    private Length equivalentFieldSize;
    /**
     * Reference depth for the TPR
     */
    private Length referenceDepth;

// --------------------- GETTER / SETTER METHODS ---------------------

    /**
     * Get the energy at which the TPR was measured
     *
     * @return Energy (MV / MeV)
     */
    public Energy getEnergy()
    {
        return energy;
    }

    /**
     * Set the energy at which the TPR was measured
     *
     * @param energy Energy (MV / MeV)
     */
    public void setEnergy(final Energy energy)
    {
        this.energy = energy;
    }

    public Length getEquivalentFieldSize()
    {
        return equivalentFieldSize;
    }

    public void setEquivalentFieldSize(Length equivalentFieldSize)
    {
        this.equivalentFieldSize = equivalentFieldSize;
    }

    public Length getReferenceDepth()
    {
        return referenceDepth;
    }

    public void setReferenceDepth(Length referenceDepth)
    {
        this.referenceDepth = referenceDepth;
    }

// ------------------------ CANONICAL METHODS ------------------------

    @Override
    public boolean equals(Object o)
    {
        if (this == o) return true;
        if (!(o instanceof Tpr)) return false;
        if (!super.equals(o)) return false;

        Tpr tpr = (Tpr) o;

        return equivalentFieldSize.compareTo(tpr.equivalentFieldSize) == 0 && referenceDepth.compareTo(
                tpr.referenceDepth) == 0 && energy.equals(tpr.energy);
    }

    @Override
    public int hashCode()
    {
        int result = super.hashCode();
        result = 31 * result + (energy != null ? energy.hashCode() : 0);
        result = 31 * result + (equivalentFieldSize != null ? equivalentFieldSize.hashCode() : 0);
        result = 31 * result + (referenceDepth != null ? referenceDepth.hashCode() : 0);
        return result;
    }

// ------------------------ INTERFACE METHODS ------------------------


// --------------------- Interface Comparable ---------------------

    public int compareTo(Tpr t)
    {
        if (equivalentFieldSize != t.equivalentFieldSize)
            return equivalentFieldSize.compareTo(t.equivalentFieldSize);
        if(size()  != t.size()) Integer.compare(size(), t.size());
        return 0;
    }

// -------------------------- OTHER METHODS --------------------------

    /**
     * Set all units of the x parameters equal
     *
     * @param unit Length unit
     */
    protected void equaliseScales(Length.Unit unit)
    {
        for (int i = 0; i < size(); i++)
        {
            at(i).getX().scale(unit);
        }
    }
}
